|
1
|
|
|
import { |
|
2
|
|
|
Body, |
|
3
|
|
|
Post, |
|
4
|
|
|
Controller, |
|
5
|
|
|
Inject, |
|
6
|
|
|
BadRequestException, |
|
7
|
|
|
UseGuards, |
|
8
|
|
|
Render, |
|
9
|
|
|
Get, |
|
10
|
|
|
Res |
|
11
|
|
|
} from '@nestjs/common'; |
|
12
|
|
|
import { Response } from 'express'; |
|
13
|
|
|
import { ICommandBus } from 'src/Application/ICommandBus'; |
|
14
|
|
|
import { LeaveRequestDTO } from '../DTO/LeaveRequestDTO'; |
|
15
|
|
|
import { CreateLeaveRequestCommand } from 'src/Application/HumanResource/Leave/Command/CreateLeaveRequestCommand'; |
|
16
|
|
|
import { LoggedUser } from '../../User/Decorator/LoggedUser'; |
|
17
|
|
|
import { IsAuthenticatedGuard } from '../../User/Security/IsAuthenticatedGuard'; |
|
18
|
|
|
import { WithName } from 'src/Infrastructure/Common/ExtendedRouting/WithName'; |
|
19
|
|
|
import { User } from 'src/Domain/HumanResource/User/User.entity'; |
|
20
|
|
|
import { getSelectableLeaveRequestTypes } from 'src/Domain/HumanResource/Leave/LeaveRequest.entity'; |
|
21
|
|
|
import { RouteNameResolver } from 'src/Infrastructure/Common/ExtendedRouting/RouteNameResolver'; |
|
22
|
|
|
|
|
23
|
|
|
@Controller('app/people/leave-requests/add') |
|
24
|
|
|
@UseGuards(IsAuthenticatedGuard) |
|
25
|
|
|
export class AddLeaveRequestController { |
|
26
|
|
|
constructor( |
|
27
|
|
|
@Inject('ICommandBus') |
|
28
|
|
|
private readonly commandBus: ICommandBus, |
|
29
|
|
|
private readonly resolver: RouteNameResolver |
|
30
|
|
|
) {} |
|
31
|
|
|
|
|
32
|
|
|
@Get() |
|
33
|
|
|
@WithName('people_leave_requests_add') |
|
34
|
|
|
@Render('pages/leave_requests/add.njk') |
|
35
|
|
|
public async get() { |
|
36
|
|
|
return { |
|
37
|
|
|
types: getSelectableLeaveRequestTypes() |
|
38
|
|
|
}; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
@Post() |
|
42
|
|
|
public async post( |
|
43
|
|
|
@Body() dto: LeaveRequestDTO, |
|
44
|
|
|
@LoggedUser() user: User, |
|
45
|
|
|
@Res() res: Response |
|
46
|
|
|
) { |
|
47
|
|
|
const { type, startDate, startsAllDay, endDate, endsAllDay, comment } = dto; |
|
48
|
|
|
|
|
49
|
|
|
try { |
|
50
|
|
|
await this.commandBus.execute( |
|
51
|
|
|
new CreateLeaveRequestCommand( |
|
52
|
|
|
user, |
|
53
|
|
|
type, |
|
54
|
|
|
startDate, |
|
55
|
|
|
startsAllDay, |
|
56
|
|
|
endDate, |
|
57
|
|
|
endsAllDay, |
|
58
|
|
|
comment |
|
59
|
|
|
) |
|
60
|
|
|
); |
|
61
|
|
|
|
|
62
|
|
|
res.redirect(303, this.resolver.resolve('people_leave_requests_list')); |
|
63
|
|
|
} catch (e) { |
|
64
|
|
|
throw new BadRequestException(e.message); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|